#! /usr/bin/env python
import os
import sys
import json
import time

start = time.time()
with open('/'.join(os.path.realpath(__file__).split('/')[0:-2]) + '/config.json') as f :
        ROOTDIR = json.load(f)['root']
if os.path.isdir(ROOTDIR)==False:
        print 'error, dir'+ROOTDIR+' not exist! '
        sys.exit(0)
if len(sys.argv)==1:
        print 'error, param not exist! '
        sys.exit(0)
if os.path.isdir(ROOTDIR+sys.argv[1])==False:
        print 'error, dir'+ROOTDIR+sys.argv[1]+' not exist!'
        sys.exit(0)

def get_zip_name(relapath):
        relapath=relapath.rstrip('/')
        while os.path.isfile(ROOTDIR+relapath+'.zip')==True:
                relapath+='_s'
        return ROOTDIR+relapath+'.zip'


LOG_NAME=ROOTDIR+sys.argv[1].rstrip('/')+"zip.log"
ZIP_NAME=get_zip_name(sys.argv[1])
DIR_NAME=ROOTDIR+sys.argv[1]+'/'
zip_command="zip -r "+ZIP_NAME+" "+DIR_NAME+" >> "+LOG_NAME
if os.system(zip_command)==0 :
        print 'zip ' + ZIP_NAME
        if os.system('rm '+DIR_NAME+' -rf')==0:
                print 'delete folder ' + DIR_NAME
        else:
                print 'error, deltree '+DIR_NAME+' failed'
else:
        print 'error, zip failed'

end = time.time()
print 'cost  ' + str((end-start)/60) + ' minutes'